package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.swanclouds.api.systems.LogEntriesService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.systems.LogEntrie;
import com.swanclouds.model.systems.LogEntrieResource;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.LogEntrieModel;
import com.swanclouds.redfish.systems.domain.LogEntrieResourceModel;

import javax.annotation.Nullable;
import java.util.List;

/**
 * @author 张立强
 * @version jdk1.8.0
 */
public class LogEntriesServiceImpl extends BaseSystemService implements LogEntriesService {

    /**
     * 查询日志集合资源信息
     *
     * @param serviceId the Log Service Id
     * @return the log entrie resource
     */
    @Override
    public LogEntrieResource get(String serviceId) {
        Preconditions.checkNotNull(serviceId, "LogService Id is null");
        return get(LogEntrieResourceModel.class, uri(ClientConstants.SYSTEMS_LOGSERVICE_ID_ENTRIES, getSystemId(), serviceId)).execute();
    }

    /**
     * 查询日志资源信息
     *
     * @param serviceId the Log Service Id
     * @param id        the log id
     * @return LogEntrie
     */
    @Override
    public LogEntrie getLogEntrie(String serviceId, String id) {
        Preconditions.checkNotNull(serviceId, "LogService Id is null");
        Preconditions.checkNotNull(id, "LogEntrie id is null");
        return get(LogEntrieModel.class, uri(ClientConstants.SYSTEMS_LOGSERVICE_ID_ENTRIES_ID, getSystemId(), serviceId,id)).execute();
    }

    /**
     * 分页查询日志资源信息
     *
     * @param serviceId the Log Service Id
     * @param offset     偏移量(跳过去多少条)
     * @param pageSize  每页显示条数 默认32条
     * @return the List LogEntrie
     */
    @Override
    public List<LogEntrie> page(String serviceId,Integer offset, @Nullable Integer pageSize) {
        Preconditions.checkNotNull(serviceId, "Log Service id is null");
        Preconditions.checkNotNull(offset, "Offset is null");
        Preconditions.checkNotNull(offset < 1 , "Offset Min is 1");
        if (pageSize == null || pageSize <= 0 || pageSize > 32) {
            pageSize = 32;
        }
        List<LogEntrie> logEntrieList = Lists.newArrayList();
        LogEntrieResourceModel resource = get(LogEntrieResourceModel.class, uri(ClientConstants.SYSTEMS_LOGSERVICE_ID_ENTRIES_ID_PAGE, getSystemId(), serviceId, offset, pageSize)).execute();
        resource.getEntries().stream().forEach(ref -> {
            logEntrieList.add(get(LogEntrieModel.class, uri(ref)).execute());
        });
        return logEntrieList;
    }

    /**
     * 查询日志资源信息集合
     *
     * @param serviceId the Log Service id
     * @return the List LogEntrie
     */
    @Override
    public List<LogEntrie> list(String serviceId) {
        Preconditions.checkNotNull(serviceId, "LogService Id is null");
        List<LogEntrie> logEntrieList = Lists.newArrayList();
        List<String> entriesList = Lists.newArrayList();
        LogEntrieResource resource = get(serviceId);
        Integer totalCount = resource.getCount();
        Integer pageSize = 32;
        Integer pages = totalCount/32;
        Integer lastPageSize = totalCount - pages * pageSize;
        for (int i = 0; i <= pages; i++) {
            if (i == pages) {
                pageSize = lastPageSize;
            }
            LogEntrieResourceModel resourceModel = get(LogEntrieResourceModel.class, uri(ClientConstants.SYSTEMS_LOGSERVICE_ID_ENTRIES_ID_PAGE, getSystemId(), serviceId, i * 32, pageSize)).execute();
            entriesList.addAll(resourceModel.getEntries());
        }
        entriesList.stream().forEach(ref -> {
            logEntrieList.add(get(LogEntrieModel.class, uri(ref)).execute());
        });
        return logEntrieList;
    }
}
